﻿Zepto(function ($) {
    FastClick.attach(document.body);
    var aid = $("#aid").val();
    var recommendSection = $('.recommend-section'),
        cateSection = $('.cate-section'),
        sortOptionArea = $('.sort-option-area'),
        couponList1 = $('#couponList1'),
        couponList2 = $('#couponList2'),
        couponListDesc = $('.coupon-list-desc'),
        loadingTxt = $('.txt-loading'),
        bannerArea = $('#bannerArea'),
        fixedBannerArea = $('.fixed-banner-area'),
        menu = $('.top-menu .inner');
    var page = 0, pagesize = 20, load = 1, cate = 0, sort = 0;
    var channel = 35;
    var _w = $('.wrapper').width(), picH = (_w - 25) / 2;

    var qs = Util.getQueryString(window.location.href);

    $('.top-menu .top-menu-item').on('click', function ()
    {
        var _this = $(this);
        if (!_this.hasClass('active')) {
            $('.top-menu .top-menu-item.active').removeClass('active');
            _this.addClass('active');

            var l = _this.offset().left - menu.offset().left;
            menu.scrollLeft(menu.scrollLeft() + l - _w / 2 + 20);

            cate = _this.data('cate-id');
            if (cate == 0) {
                sortOptionArea.css('display', 'none');
                cateSection.css('display', 'none');
                recommendSection.css('display', 'block');
            } else {
                sortOptionArea.css('display', 'block');
                recommendSection.css('display', 'none');
                cateSection.css('display', 'block');
            }

            sort = 0;
            $('.sort-option-area li.active').removeClass('active');
            $($('.sort-option-area li')[0]).addClass('active');

            page = 0;
            load = 1;

            couponList1.html('');
            couponList2.html('');

            $('body').scrollTop(0);
            loadingTxt.html('努力获取中...');
            getProductList();
        }
    });


    $('.sort-option-area li').on('click', function () {
        var _this = $(this);
        if (!_this.hasClass('active')) {
            $('.sort-option-area li.active').removeClass('active');
            _this.addClass('active');
            sort = _this.data('type');
            page = 0;
            load = 1;
            couponList2.html('');
            $('body').scrollTop(0);
            loadingTxt.html('努力获取中...');

            getProductList();
        }
    });


    if (!(1 * qs.bh)) {
        var bannerHtml = '';
        for (var i = 0; i < banner.length; i++) {
            var b = banner[i];
            if (b.element_type != 'webview') {
                continue;
            }
            bannerHtml += '<a data-back-url="' + b.extend + '" href="' + b.extend + '" class="swiper-slide">';
            bannerHtml += '<img class="lazy" data-original="' + b.pic + '">';
            bannerHtml += '</a>';
        }
        bannerArea.css('height', (_w * 240 / 750) + 'px').html(bannerHtml);
        var sw = new Swiper('.swiper-container', {
            autoplay: banner.length > 1 ? 3000 : false,
            loop: true,
            pagination: '.swiper-pager'
        });
    }

    if (!(1 * qs.fh)) {
        var fixedBannerHtml = '';
        var fb_h = _w * 340 / 750;
        for (var i = 0; i < (fixedBanner.length > 4 ? 4 : fixedBanner.length) ; i++) {
            var b = fixedBanner[i];
            if (b.element_type != 'webview') {
                continue;
            }
            var s = '';
            switch (i * 1) {
                case 0:
                    s = 'width:' + (fb_h * 300 / 340 - 1) + 'px;height:' + fb_h + 'px;border-right:1px solid #eee;'
                    break;
                case 1:
                    s = 'width:' + (_w - fb_h * 300 / 340) + 'px;height:' + ((_w - fb_h * 300 / 340) * 170 / 450 - 1) + 'px;border-bottom:1px solid #eee;'
                    break;
                case 2:
                    s = 'width:' + ((_w - fb_h * 300 / 340) / 2 - 1) + 'px;height:' + (fb_h - (_w - fb_h * 300 / 340) * 170 / 450) + 'px;border-right:1px solid #eee;'
                    break;
                case 3:
                    s = 'width:' + (_w - fb_h * 300 / 340) / 2 + 'px;height:' + (fb_h - (_w - fb_h * 300 / 340) * 170 / 450) + 'px;'
                    break;
            }
            fixedBannerHtml += '<a data-back-url="' + b.extend + '" href="' + b.extend + '" class="fixed-banner-item fbi' + i + '" style="' + s + '">';
            fixedBannerHtml += '<img class="lazy" data-original="' + b.pic + '">';
            fixedBannerHtml += '</a>';
        }
        fixedBannerArea.css('height', fb_h + 'px').html(fixedBannerHtml);
    }

    couponListDesc.html('<b></b><i></i><span>每天早10点 · 晚8点上新</span><i></i><b></b>');

    Util.lazyLoad('lazy');

    if (qs.dt && qs.dt != 1) {
        qs.dt = qs.dt * 1;
        if (qs.dt >= 1 && qs.dt <= $('.top-menu .top-menu-item').length) {
            $($('.top-menu .top-menu-item')[qs.dt - 1]).trigger('click');
        }
    } else {
        getProductList();
    }




    var de = document.documentElement;
    $(document).on('scroll', function () {
        var sH = de.scrollHeight, cH = de.clientHeight;
        if ($('body').scrollTop() > 250) {
            $('.user-footer').css('display', 'none');
        } else {
            $('.user-footer').css('display', 'block');
        }
        if (sH - cH - $('body').scrollTop() <= 400) {
            getProductList();
        }
    });
    function getProductList() {
        if (!load) {
            return;
        }
        $.ajax({
            url: '/' + aid + '/getZhekouListByCate.html',
            type: 'Post',
            dataType: 'json',
            data: {
                'limit': page,
                'pagesize': pagesize,
                'cid': cate,
                'sort': sort
            },
            beforeSend: function () {
                $(".loading-area").show();
                $(".loading-img").show();

                load = 0;
            },
            complete: function () {
                $(".loading-area").hide();
                $(".loading-img").hide();
            },
            success: function (data) {
                if (data.rows.CurrentPageList != null) {
                    createProductList(data.rows.CurrentPageList);
                    if (data.rows.CurrentPageList.length >= pagesize) {

                        page++;
                        load = 1;
                    } else {
                        loadingTxt.html('没有更多商品了');
                    }
                }
            }
        });
    }

    function createProductList(clist) {
        if (cate == 0) {
            var htmlstr = '';
            for (var i = 0, len = clist.length; i < len; i++) {
                //if (clist[i].feed_type != 1) {
                //    continue;
                //}
                var zk = clist[i];
                var platform = '', platformstr = '';
                switch (zk.userType * 1) {
                    case 2:
                        platform = '淘宝';
                        platformstr = 'taobao';
                        break;
                    case 1:
                        platform = '天猫';
                        platformstr = 'tmall';
                        break;
                }
                htmlstr += '<a  href="/' + aid + '/goods/' + zk.auctionId + '.html">';
                htmlstr += '<div class="coupon-card-v2">';
                htmlstr += '<div class="pic-area lazy new" data-original="' + zk.pictUrl + '_400x400"></div>';
                htmlstr += '<div class="info-area">';
                htmlstr += '<div class="info-top">';
                htmlstr += '<p class="title">' + zk.title + '</p>';
                htmlstr += '<p class="stat">';
                //if (zk.post_free) {
                    htmlstr += '<span class="platform ' + platformstr + '"></span>';
                //}
                htmlstr += platform + '价&nbsp;&yen;' + zk.zkPrice + '</p>';
                htmlstr += '</div>';
                htmlstr += '<div class="info-bottom">';
                htmlstr += '<div class="price-area">';
                htmlstr += '<span class="qhj">券后价</span>';
                htmlstr += '<span class="yen">&yen;</span><span class="price number-font">' + (zk.zkPrice - zk.couponAmount).toFixed(1) + '</span>';
                htmlstr += '<span class="quan-btn"><em class="number-font">' + zk.couponAmount + '</em>元券</span>';
                htmlstr += '</div>';
                htmlstr += '<div class="progress-area">';
                htmlstr += '<span class="pro-desc">已领' + Math.floor(zk.couponTotalCount * 100) + '%</span>';
                htmlstr += '<div class="pro-container"><div class="pro" style="width:' + Math.floor(zk.couponTotalCount * 100) + '%;"></div>';
                htmlstr += '</div>';
                htmlstr += '</div>';
                htmlstr += '</div>';
                htmlstr += '</div>';
                htmlstr += '</div>';
                htmlstr += '</a>';
            }
            couponList1.append(htmlstr);
        } else {
            var htmlstr = '';
            for (var i = 0, len = clist.length; i < len; i++) {
                var zk = clist[i];
                htmlstr += '<a  href="/' + aid + '/goods/' + zk.auctionId + '.html">';
                htmlstr += '<div class="product-item-t2" style="width:' + picH + 'px;">';
                htmlstr += '<div class="pic lazy new" data-original="' + zk.pictUrl + '_400x400" style="width: ' + picH + 'px; height: ' + picH + 'px;"></div>';
                htmlstr += '<p class="title">';
                //if (zk.post_free == 1) {
                htmlstr += '<span class="post-free">包邮</span>&nbsp;';
                // }
                htmlstr += zk.title + '</p>';

                htmlstr += '<div class="progress-area">';
                htmlstr += '<p class="pro-text">已领' + Math.floor(zk.couponTotalCount * 100) + '%</p>';
                htmlstr += '<div class="pro-container">';
                htmlstr += '<div data-pro="' + zk.couponTotalCount + '" class="pro" style="width:' + Math.floor(zk.couponTotalCount * 100) + '%;"></div>';
                htmlstr += '</div>';
                htmlstr += '</div>';

                htmlstr += '<div class="price-area">';
                htmlstr += '<p class="gap-price"><em class="number-font">' + zk.couponAmount + '</em>元券</p>';
                htmlstr += '<p class="price">券后价&yen;<em class="number-font">' + (zk.zkPrice - zk.couponAmount).toFixed(1) + '</em></p>';
                htmlstr += '</div>';
                htmlstr += '</div>';
                htmlstr += '</a>';
            }
            couponList2.append(htmlstr);
        }
        //backUrlEvent();
        Util.lazyLoad('lazy.new');
        $('.lazy.new').removeClass('new');
    }
});